package com.wtwd.campus.wx.controller;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.service.WxService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author ldaoliang
 * @Date create in 10:36 2021/10/20
 * @Description 喜马拉雅API
 */
@RestController
@RequestMapping("/xmly")
public class XmlyApiController {

    private static final Logger logger = LoggerFactory.getLogger(XmlyApiController.class);

    @Autowired
    private WxService wxService;

    /**
     * 根据传过来的OpenId获取该openId对应账号的学生信息列表
     * @return
     */
    @GetMapping("/getStudentListByOpenId")
    public RespEntity getStudentListByOpenId(@RequestParam("openId")String openId){
        logger.info("getStudentListByOpenId : openId = {}",openId);
        if(StringUtils.isBlank(openId)){
            return new RespEntity(RespCode.PARAM_OPENID_CANNOT_NULL);
        }
        // 1.根据openId从wx_user表中查询openId对应的账号；可能未找到则情况为未登录或者被别人挤下去了
        Integer wxUserId = wxService.getWxUserIdByOpenId(openId);
        if(wxUserId == null){
            return new RespEntity(RespCode.WX_UESR_NOT_LOGIN);
        }
        // 2.查询列表数据返回
        List<Map<String,Object>> result = wxService.getXMLYStudentList(wxUserId);
        return new RespEntity(RespCode.SUCCESS,result);
    }

}
